package view.ui.minimap
{
	import control.manager.BitmapManager;
	import control.manager.LayerManager;
	import control.manager.MapManager;
	import control.manager.RoleManager;
	
	import data.GameConst;
	import data.MapData;
	
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import view.layer.BaseLayer;
	import view.layer.GameLayer;
	import view.layer.MapLayer;
	import view.item.MapTileItem;
	import view.role.MapPlayer;

	/**
	 * 小地图显示ui，点击寻路用 
	 * @author leo
	 * 
	 */	
	public class MiniMapContainer extends BaseLayer
	{
		public static var layerName :String = "MiniMapContainer";
		/**
		 * 遮罩的尺寸 
		 */		
		private var miniVisibleSize :int = 130;
		/**
		 * 小地图位图
		 */		
		public var miniMap :Bitmap;
		/**
		 * 小地图人物层 
		 */		
		public var miniPlayerLayer :MiniPlayerLayer;
		/**
		 * 小地图容器 
		 */		
		private var mapContainer :Sprite;
		/**
		 * 路径层 
		 */		
		private var pathLayer :Shape;
		/**
		 * 小地图遮罩 
		 */		
		private var miniMapMask :Shape;
		/**
		 * 人物点 
		 */		
		private var playerPoint :Shape;
		
		public function MiniMapContainer()
		{
			super();
			initMiniMap();
		}
		
		/**
		 * 根据寻路数组画路径
		 * @param path
		 * 
		 */		
		public function drawPath(path :Array):void
		{
			if(path.length > 0)
			{
				var g :Graphics = this.pathLayer.graphics;
				var p :Point = path[0];
				var mapData :MapData = MapManager.instance.curMapData;
				var w :int = mapData.mapWalkGapWidth;
				var h :int = mapData.mapWalkGapHeight;
				var tp :Point = MapManager.instance.getPixelPoint(w,h,p.x,p.y);
				g.clear();
				g.lineStyle(2,0xffff00);
				g.moveTo(tp.x/GameConst.MAP_SCALE,tp.y/GameConst.MAP_SCALE);
				for(var i :int = 1; i < path.length; i++)
				{
					p = path[i];
					tp = MapManager.instance.getPixelPoint(w,h,p.x,p.y);
					g.lineTo(tp.x/GameConst.MAP_SCALE,tp.y/GameConst.MAP_SCALE);
				}
			}
		}
		/**
		 * 清空路径层
		 */		
		public function clearPath():void
		{
			if(this.pathLayer)
			{
				this.pathLayer.graphics.clear();
			}
		}
		/**
		 * 锁定人物在中心点 
		 * @param posX
		 * @param posY
		 * 
		 */		
		public function lockAt(posX :Number,posY :Number):void
		{
			//人物点位置
			this.playerPoint.x = posX;
			this.playerPoint.y = posY;
			
			
			//中心位置
			var layerPosX :Number = miniVisibleSize/2 - posX;
			var layerPosY :Number = miniVisibleSize/2 - posY;
			var mapData :MapData = MapManager.instance.curMapData;
			var miniMapW :int = mapData.mapWidth / GameConst.MAP_SCALE;
			var miniMapH :int = mapData.mapHeight / GameConst.MAP_SCALE;
			//左边界
			if(layerPosX > 0)
			{
				layerPosX = 0;
			}else if(layerPosX < miniVisibleSize - miniMapW)  //右边界
			{
				layerPosX = miniVisibleSize - miniMapW;
			}
			
			//上边界
			if(layerPosY > 0)
			{
				layerPosY = 0;
			}else if(layerPosY < miniVisibleSize - miniMapH)  //下边界
			{
				layerPosY = miniVisibleSize - miniMapH;
			}
			//更新位置显示
			this.updatePosition(new Point(layerPosX,layerPosY));
		}
		
		
		override public function updatePosition(movePoint:Point=null, isResize:Boolean=false):void
		{
			//层块移动
			var p :Point;
			if(movePoint)
			{
				p = movePoint;
			}
			
			if(this.moveArea)
			{
				if(p.x > moveArea.right)
				{
					p.x = moveArea.right;
				}
				
				if(p.y > moveArea.bottom)
				{
					p.y = moveArea.bottom;
				}
			} 
			
			this.miniMap.x = p.x;
			this.miniMap.y = p.y;
			this.miniPlayerLayer.x = p.x;
			this.miniPlayerLayer.y = p.y;
			
			//重置
			this.sceneMoveSpeed = new Point();
		}
		
		/**
		 * 初始化小地图
		 * 
		 */		
		public function initMiniMap():void
		{
			removeDisplay();
			
			//小地图
			this.miniMap = new Bitmap();
			//小地图容器
			this.mapContainer = new Sprite();
			var mapData :MapData = MapManager.instance.curMapData;
			var uid :String = "map_" + mapData.mapID + "_" + "pre";
			var bmp :MapTileItem = BitmapManager.instance.findMapTileItem(uid);
			if(bmp)
			{
				this.miniMap.bitmapData = bmp.bitmapData;
			}
			this.mapContainer.addChild(this.miniMap);
			this.addChild(this.mapContainer);
			
			//小地图遮罩
			this.miniMapMask = new Shape();
			this.miniMapMask.graphics.lineStyle(1,0xff0000);
			this.miniMapMask.graphics.beginFill(0,0);
			this.miniMapMask.graphics.drawCircle(miniVisibleSize/2,miniVisibleSize/2,miniVisibleSize/2);
			this.miniMapMask.graphics.endFill();
			this.mapContainer.addChild(this.miniMapMask);
			
			this.mapContainer.mask = this.miniMapMask;
			initPlayerLayer();
			
			//锁定人物在小地图中心
			var gameLayer :GameLayer = LayerManager.instance.getLayer(GameLayer.NAME) as GameLayer;
			var playerX :Number = gameLayer.player.x / GameConst.MAP_SCALE;
			var playerY :Number = gameLayer.player.y / GameConst.MAP_SCALE;
			var miniMapW :int = mapData.mapWidth / GameConst.MAP_SCALE;
			var miniMapH :int = mapData.mapHeight / GameConst.MAP_SCALE;
			this.moveArea = new Rectangle(0,0,miniMapW,miniMapH);
			this.lockAt(playerX,playerY);
			
			addEventListener(MouseEvent.CLICK,clickHandler);
		}
		/**
		 * 点击小地图寻路 
		 * @param event
		 * 
		 */		
		private function clickHandler(event :MouseEvent):void
		{
			var p :Point = new Point(event.stageX,event.stageY);
			var miniMapP :Point = this.miniMap.globalToLocal(p);
			trace(miniMapP);
			var mapP :Point = new Point(miniMapP.x * GameConst.MAP_SCALE,miniMapP.y * GameConst.MAP_SCALE);
			var gameLayer :GameLayer = LayerManager.instance.getLayer(GameLayer.NAME) as GameLayer;
			var player :MapPlayer = gameLayer.player;
			player.findPath(new Point(player.x,player.y),mapP);
		}
		
		private function initPlayerLayer():void
		{
			//人物层
			this.miniPlayerLayer = new MiniPlayerLayer();
			this.miniPlayerLayer.width = this.miniMap.width;
			this.miniPlayerLayer.height = this.miniMap.height;
			//路径层
			this.pathLayer = new Shape();
			this.miniPlayerLayer.addChild(this.pathLayer);
			
			//人物点
			this.playerPoint = new Shape();
			this.playerPoint.graphics.beginFill(0xff0000,0.8);
			this.playerPoint.graphics.drawCircle(0,0,3);
			this.playerPoint.graphics.endFill();
			this.miniPlayerLayer.addChild(this.playerPoint);
			this.mapContainer.addChild(this.miniPlayerLayer);
			
		}
		/**
		 * 切换地图时清空所有图层
		 */		
		private function removeDisplay():void
		{
			if(this.miniMap)
			{
				this.mapContainer.removeChild(this.miniMap);
				this.miniMap = null;
			}
			
			if(this.miniMapMask)
			{
				this.mapContainer.removeChild(this.miniMapMask);
				this.miniMapMask = null;
			}
			
			if(this.playerPoint)
			{
				this.miniPlayerLayer.removeChild(this.playerPoint);
				this.playerPoint = null;
			}
			if(this.pathLayer)
			{
				this.miniPlayerLayer.removeChild(this.pathLayer);
				this.pathLayer = null;
			}
			if(this.miniPlayerLayer)
			{
				this.mapContainer.removeChild(this.miniPlayerLayer);
				this.miniPlayerLayer = null;
			}
			
			if(this.mapContainer)
			{
				this.removeChild(this.mapContainer);
				this.mapContainer = null;
			}
		}
	}
}